package com.dyna.digit.process.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.StopWatch;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyna.constants.DictTypeConstant;
import com.dyna.core.utils.ObjectUtil;
import com.dyna.digit.process.domain.entity.DigitProject;
import com.dyna.digit.process.domain.entity.DigitUnitInfo;
import com.dyna.digit.process.domain.query.DigitProjQuery;
import com.dyna.digit.process.domain.resp.DigitOverProjResp;
import com.dyna.digit.process.domain.resp.DigitProjectPage;
import com.dyna.digit.process.mapper.DigitProjectMapper;
import com.dyna.digit.process.service.DigitProjectService;
import com.dyna.domain.resp.PageResult;
import com.dyna.domain.resp.R;
import com.dyna.enums.DigitFlowStatusEnum;
import com.dyna.enums.DigitProjectTypeEnum;
import com.dyna.enums.DigitUnitTypeEnum;
import com.dyna.foundation.domain.entity.SysDictData;
import com.dyna.foundation.feign.FoundationDictApi;
import com.dyna.foundation.utils.DictUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @author Admin
* @description 针对表【digit_project】的数据库操作Service实现
* @createDate 2023-11-11 16:34:20
*/
@Slf4j
@Service
public class DigitProjectServiceImpl extends ServiceImpl<DigitProjectMapper, DigitProject>
    implements DigitProjectService{

    private final DigitProjectMapper digitProjectMapper;
    private final FoundationDictApi foundationDictApi;

    @Autowired
    public DigitProjectServiceImpl(DigitProjectMapper digitProjectMapper,
                                   FoundationDictApi foundationDictApi
    ) {
        this.digitProjectMapper = digitProjectMapper;
        this.foundationDictApi = foundationDictApi;
    }

    @Override
    public PageResult<DigitProjectPage> getPageList(DigitProjQuery digitProjQuery) {
        List<DigitProjectPage> pageList = new ArrayList<>();
        StopWatch watch = new StopWatch();
        watch.start();
        Long count = digitProjectMapper.queryDigitProjectCount(digitProjQuery);
        watch.stop();
        log.info("项目列表数量查询耗时:::{}", watch.getTotalTimeMillis());
        if (0L != count) {
            StopWatch watch1 = new StopWatch();
            watch1.start();
            pageList = digitProjectMapper.queryDigitProjectPage(digitProjQuery);
            watch1.stop();
            log.info("项目列表查询耗时:::{}", watch1.getTotalTimeMillis());
            this.toTextHandler(pageList);
        }
        return new PageResult<>(pageList, count);
    }

    private void toTextHandler(List<DigitProjectPage> pageList) {
        List<String> dictTypeList = Arrays.asList(
                DictTypeConstant.PROJ_BUILD_CATEGORY,
                DictTypeConstant.PROJ_YWL_BUILD_CATEGORY,
                DictTypeConstant.JSL_DECLARE_FLOW,
                DictTypeConstant.YWL_DECLARE_FLOW);
        // 加载主管单位
        List<DigitUnitInfo> unitInfoList = digitProjectMapper.queryUnitList();
        // 加载字典数据
        R<Map<String, List<SysDictData>>> mapR = foundationDictApi.getBatchSysDictList(dictTypeList);
        // 建设/运维类别
        List<SysDictData> jslBuildCategoryList = mapR.getData().get(DictTypeConstant.PROJ_BUILD_CATEGORY);
        List<SysDictData> ywlBuildCategoryList = mapR.getData().get(DictTypeConstant.PROJ_YWL_BUILD_CATEGORY);
        jslBuildCategoryList.addAll(ywlBuildCategoryList);
        Map<Integer, String> buildCategoryMap = DictUtil.toDictLabelMap(jslBuildCategoryList);
        // 申报环节
        Map<String, String> jslFlow = DictUtil.toDictValueLabelMap(mapR.getData().get(DictTypeConstant.JSL_DECLARE_FLOW));
        Map<String, String> ywlFlow = DictUtil.toDictValueLabelMap(mapR.getData().get(DictTypeConstant.YWL_DECLARE_FLOW));

        for (DigitProjectPage project : pageList) {
            // 映射主管单位text
            String unitName = unitInfoList.stream()
                    .filter(f -> DigitUnitTypeEnum.CURRENT.getType().equals(f.getType()))
                    .filter(f -> f.getId().equals(project.getBaseManDept()))
                    .findFirst()
                    .map(DigitUnitInfo::getUnitName)
                    .orElse(null);
            project.setBaseManDeptText(unitName);
            // 映射建设/运维类别text
            String projBuildCategoryText = buildCategoryMap.get(project.getProjBuildCategory());
            project.setProjBuildCategoryText(projBuildCategoryText);
            // 当前环节
            if (ObjectUtil.isNotNull(project.getFlowStatus())) {
                if (DigitProjectTypeEnum.JSL.getValue().equals(project.getProjectType())) {
                    String flowName = jslFlow.getOrDefault(Convert.toStr(project.getFlowStatus()), null);
                    project.setFlowStatusText(flowName);
                } else if (DigitProjectTypeEnum.YWL.getValue().equals(project.getProjectType())) {
                    project.setFlowStatusText(ywlFlow.getOrDefault(Convert.toStr(project.getFlowStatus()), null));
                }else {
                    project.setFlowStatusText(null);
                }
            }
        }
    }

    @Override
    public List<DigitUnitInfo> getUnitListByType(String type) {
        return digitProjectMapper.queryUnitListByType(type);
    }

    @Override
    public List<DigitProjectPage> testDigitPageQPS(DigitProjQuery digitProjQuery) {
        return digitProjectMapper.queryDigitProjectPage(digitProjQuery);
    }

    @Override
    public Long testDigitCountQPS(DigitProjQuery digitProjQuery) {
        return digitProjectMapper.queryDigitProjectCount(digitProjQuery);
    }

    @Override
    public List<DigitOverProjResp> getOverProjList() {
        return digitProjectMapper.queryOverProjList();
    }

    @Override
    public Boolean startDeclare(Integer projectId) {
        LambdaUpdateWrapper<DigitProject> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(DigitProject::getFlowStatus, DigitFlowStatusEnum.DEPT_AUDIT.getStatus());
        updateWrapper.eq(DigitProject::getProjectId, projectId);
        return this.update(updateWrapper);
    }

    @Override
    public DigitProject detail(Integer id) {
        return digitProjectMapper.detail(id);
    }

}









